package d10_interface_demo;

import java.util.ArrayList;

public class ClassDataImpl2 implements ClassData {
    private ArrayList<Student> students;

    public ClassDataImpl2(ArrayList<Student> students) {
        this.students = students;
    }

    /**
     * 第二套方案
     */
    @Override
    public void printAllStudentInfos() {
        System.out.println("===展示全部学生的信息===");
        int count = 0;
        //遍历集合
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student.getName() + " " + student.getSex() + " " + student.getScore());
            if (student.getSex() == '男') count++;
        }
        System.out.println("男人人数:" + count);
        System.out.println("女人人数:" + (students.size() - count));

    }

    //打印学生平均成绩
    @Override
    public void printAllStudentAverageScore() {
        System.out.println("===展示全部学生的平均分===");
        Student s1 =  students.get(0);
        //为了更好的性能,遍历从1开始,先记忆总分
        double allScore = s1.getScore();
        double score = s1.getScore();
        double max =score;
        double min =score;
        //遍历集合
        for (int i = 1; i < students.size(); i++) {
            Student s = students.get(i);
            double score1 = s.getScore();
            allScore += score1;
            //判断最低与最高分
            if (max < score1) max = score1;
            if (min > score1) min = score1;
        }
        //打印平均分
        System.out.println("最低分是:" + min);
        System.out.println("最高分是:" + max);
        System.out.println("平均分是:" + ((allScore - min - max) / (students.size() - 2)));

    }
}
